/**
 * @Author: douxuefeng
 * @Date: 2020/9/7 下午1:46
 * @File: util
 * @Desc:
 */
package pay

import (
	"bytes"
	"io/ioutil"
	"net/http"
	"sort"
	"strconv"
	"strings"
	"time"
)

func GetWechatPaySignString(m map[string]interface{}, key string) string {
	var params []string
	delete(m, "XMLName")
	for i, v := range m{
		if v != nil {
			params = append(params, i)
		}
	}
	sort.Strings(params)
	var buf strings.Builder
	for _, v := range params {
		if value, ok := m[v]; ok {
			buf.WriteString(v)
			buf.WriteByte('=')
			if _, ok := value.(float64); ok {
				buf.WriteString(strconv.FormatFloat(value.(float64), 'f', -1,64))
			} else {
				buf.WriteString(value.(string))
			}

			buf.WriteByte('&')
		}
	}
	buf.WriteString("key")
	buf.WriteByte('=')
	buf.WriteString(key)
	return buf.String()
}
func PostXml(url string, data []byte) []byte {
	client := &http.Client{
		Timeout: 10 * time.Second,
	}
	req, _ := http.NewRequest("POST", url, bytes.NewBuffer(data))
	req.Header.Set("Content-Type", "application/xml;charset=utf-8")
	resp, _ := client.Do(req)
	defer resp.Body.Close()
	rs, _ := ioutil.ReadAll(resp.Body)
	return rs
}